package diploma.kisel.modelas.domain;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 * Domain class for Program (table - program).
 * 
 * @author Nikolay Kisel
 *
 */
@Entity
@Table(name = "program")
@NamedQueries({
	@NamedQuery(name = "Program.findAll", query = "select p from Program p"),
	@NamedQuery(name = "Program.findByPrice", query = "select p from Program p where p.cost >= :minPrice and p.cost <= :maxPrice"),
	@NamedQuery(name = "Program.findById", query = "select p from Program p where p.id = :id"),
	@NamedQuery(name = "Program.findByPriceAndType", query = "select p from Program p where p.cost >= :minPrice and p.cost <= :maxPrice and p.programType = :type")})
public class Program {

	private Integer id;
	private String number;
	private String name;
	private String version;
	private ProgramType programType;
	private Integer cost;
	private String requirements;

	// for many to many mapping
	private Set<Workplace> workplaces = new HashSet<Workplace>();
	
	public Program() {
		
	}	

	public Program(String number, String name, String version,
			ProgramType programType, Integer cost, String requirements) {
		this.number = number;
		this.name = name;
		this.version = version;
		this.programType = programType;
		this.cost = cost;
		this.requirements = requirements;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id_program")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "number")
	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	@Column(name = "name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "version")
	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	@Column(name = "type")
	@Enumerated(EnumType.STRING)
	public ProgramType getProgramType() {
		return programType;
	}

	public void setProgramType(ProgramType programType) {
		this.programType = programType;
	}

	@Column(name = "cost")
	public Integer getCost() {
		return cost;
	}

	public void setCost(Integer cost) {
		this.cost = cost;
	}

	@Column(name = "requirements")
	public String getRequirements() {
		return requirements;
	}

	public void setRequirements(String requirements) {
		this.requirements = requirements;
	}

	@ManyToMany
	@JoinTable(name = "list_program", joinColumns = @JoinColumn(name = "id_program"), inverseJoinColumns = @JoinColumn(name = "id_workplace"))
	public Set<Workplace> getWorkplaces() {
		return workplaces;
	}

	public void setWorkplaces(Set<Workplace> workplaces) {
		this.workplaces = workplaces;
	}	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((number == null) ? 0 : number.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Program other = (Program) obj;
		if (number == null) {
			if (other.number != null)
				return false;
		} else if (!number.equals(other.number))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Program [id=" + id + ", number=" + number + ", name=" + name
				+ ", version=" + version + ", programType=" + programType
				+ ", cost=" + cost + ", requirements=" + requirements + "]";
	}

}
